package com.agriculture.service;

import com.agriculture.mapper.InventoryMapper;
import com.agriculture.domain.Inventory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.math.BigDecimal;
import java.util.stream.Collectors;
import java.util.HashMap;
import com.agriculture.common.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
public class InventoryService {
    private static final Logger log = LoggerFactory.getLogger(InventoryService.class);

    @Autowired
    private InventoryMapper inventoryMapper;

    public PageResult<Inventory> getStockList(Long ownerId, String search, int page, int pageSize) {
        if (ownerId == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }
        
        int offset = (page - 1) * pageSize;
        
        List<Inventory> inventoryList = inventoryMapper.getStockList(ownerId, search, offset, pageSize);
        
        int total = inventoryMapper.getStockCount(ownerId, search);
        
        log.info("查询到库存记录: total={}, page={}, pageSize={}, records={}", 
            total, page, pageSize, inventoryList.size());
            
        return new PageResult<>(inventoryList, total);
    }

    private String formatCategory(String category) {
        Map<String, String> categoryMap = new HashMap<>();
        categoryMap.put("grain", "粮食");
        categoryMap.put("vegetable", "蔬菜");
        categoryMap.put("fruit", "水果");
        categoryMap.put("poultry", "禽类");
        return categoryMap.getOrDefault(category, category);
    }
} 